{
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.5"
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python395jvsc74a57bd0d21333ff62c43b8d3f22f2fffa3b7389f580599dfc1ab4fc74fc1532be50abca",
   "display_name": "Python 3.9.5 64-bit"
  },
  "metadata": {
   "interpreter": {
    "hash": "d21333ff62c43b8d3f22f2fffa3b7389f580599dfc1ab4fc74fc1532be50abca"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "Sam 说: 我 10 岁了，我在读 3 年级\n"
     ]
    }
   ],
   "source": [
    "# 子类（也叫派生类）会继承父类（也叫基类）的属性和方法\n",
    "# 定义类\n",
    "class people:\n",
    "    # 定义基本属性\n",
    "    name = ''\n",
    "    age = 0\n",
    "    # 这个是私有属性，在类外部无法访问：\n",
    "    __weight = 0\n",
    "    # 构造方法\n",
    "    def __init__(self,n,a,w):\n",
    "        self.name = n\n",
    "        self.age = a\n",
    "        self.__weight = w\n",
    "    def speak(self):\n",
    "        print(\"%s 说: 我 %d 岁。\" %(self.name,self.age))\n",
    "\n",
    "# 单继承示例：\n",
    "class student(people):\n",
    "    grade = ''\n",
    "    def __init__(self,n,a,w,g):\n",
    "        # 调用父类的构造方法：\n",
    "        people.__init__(self,n,a,w)\n",
    "        self.grade = g\n",
    "    # 覆写父类的方法：    \n",
    "    def speak(self):\n",
    "        print(\"%s 说: 我 %d 岁了，我在读 %d 年级\"%(self.name,self.age,self.grade))  \n",
    "\n",
    "s = student('Sam',10,60,3)\n",
    "s.speak()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "Armi 说: 我 25 岁了，我在读 4 年级\n"
     ]
    }
   ],
   "source": [
    "# 下面是多继承示例：\n",
    "class people:\n",
    "    # 定义基本属性\n",
    "    name = ''\n",
    "    age = 0\n",
    "    # 这个是私有属性，在类外部无法访问：\n",
    "    __weight = 0\n",
    "    # 构造方法\n",
    "    def __init__(self,n,a,w):\n",
    "        self.name = n\n",
    "        self.age = a\n",
    "        self.__weight = w\n",
    "    def speak(self):\n",
    "        print(\"%s 说: 我 %d 岁。\" %(self.name,self.age))\n",
    "#单继承示例\n",
    "class student(people):\n",
    "    grade = ''\n",
    "    def __init__(self,n,a,w,g):\n",
    "        #调用父类的构函\n",
    "        people.__init__(self,n,a,w)\n",
    "        self.grade = g\n",
    "    #覆写父类的方法\n",
    "    def speak(self):\n",
    "        print(\"%s 说: 我 %d 岁了，我在读 %d 年级\"%(self.name,self.age,self.grade))\n",
    "\n",
    "# 另一个类，多重继承之前的准备：\n",
    "class speaker():\n",
    "    topic = ''\n",
    "    name = ''\n",
    "    def __init__(self,n,t):\n",
    "        self.name = n\n",
    "        self.topic = t\n",
    "    def speak(self):\n",
    "        print(\"我叫 %s,我是一个演说家，我的演讲主题是 %s\"%(self.name,self.topic)) \n",
    "\n",
    "# 多重继承：\n",
    "class sample(student,speaker):\n",
    "    a = ''\n",
    "    def __init__(self,n,a,w,g,t):\n",
    "        student.__init__(self,n,a,w,g)\n",
    "        speaker.__init__(self,n,t)\n",
    "\n",
    "# 调用\n",
    "test = sample(\"Armi\",25,80,4,'Python')\n",
    "test.speak()  # 方法同名，则默认调用的是在括号中排前面的父类的方法。                 \n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "调用了子类的方法\n调用了父类的方法\n"
     ]
    }
   ],
   "source": [
    "# 方法重写\n",
    "# 父类的方法功能不能满足需求，可以在子类中重写父类的方法。\n",
    "class Parent:\n",
    "    def myMethod(self):\n",
    "        print(\"调用了父类的方法\")\n",
    "class child(Parent):\n",
    "    def myMethod(self):\n",
    "        print(\"调用了子类的方法\")\n",
    "c = child() # 子类实例  \n",
    "c.myMethod() # 子类调用重写方法  \n",
    "# ◆★★★★★★用子类对象调用父类已经被覆盖的方法  ★★★★★★\n",
    "super(child,c).myMethod()           "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# super函数是用于调用父类（超类）的一个方法。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ]
}